<?php

include 'secure.php';

function string_fix($string,$remove='',$caseSens=1)
	//remove problem characters from a string as well as a provided string
{
	if($caseSens==1)
		{$string = trim(str_replace($remove, '', $string));}//if case sensitive is on, do a case sensitive search
	else if ($caseSens==0)
		{$string = trim(str_ireplace($remove, '', $string));}//otherwise case insensitive
	$string = trim(str_ireplace('[', '', $string));
	$string = trim(str_ireplace(']', '', $string));
	$string = trim(str_ireplace('{', '', $string));
	$string = trim(str_ireplace('}', '', $string));
	$string = trim(str_ireplace('=', '', $string));
	$string = trim($string);
	return $string;
}

function string_chop($string,$chop)
	//return all of a string up to a specified character
{
	$returnString = $string;//prepare to return the original string
	$chopPos = strpos($string, $chop);
	if ($chopPos<>FALSE)
		//if chop character is found, return everything up to it
		{
		$returnString = trim(substr($string, 0,$chopPos));
	}
	return $returnString;
}

function wiki_caps($searchString)
	//capitalize a string for wikipedia searching
{
	//capitalize everything first
	$searchString = ucwords($searchString);
	//split title into array
	$searchStringArray = explode(' ', $searchString);
	//remove first and last words
	//first and last word should stay capitalized
	$firstWord = array_shift($searchStringArray);
	$lastWord = array_pop($searchStringArray);

	//glue back together without first and last words
	$rebuiltString = implode(' ', $searchStringArray);//remerge the array. Now we have a title with the first word removed

	//replace words that should be lowercase
	$rebuiltString = str_replace('A', 'a', $rebuiltString);
	$rebuiltString = str_replace('An', 'an', $rebuiltString);
	$rebuiltString = str_replace('The', 'the', $rebuiltString);
	$rebuiltString = str_replace('To', 'to', $rebuiltString);
	$rebuiltString = str_replace('And', 'and', $rebuiltString);
	$rebuiltString = str_replace('But', 'but', $rebuiltString);
	$rebuiltString = str_replace('Or', 'or', $rebuiltString);
	$rebuiltString = str_replace('Nor', 'nor', $rebuiltString);
	$rebuiltString = str_replace('For', 'for', $rebuiltString);
	$rebuiltString = str_replace('Yet', 'yet', $rebuiltString);
	$rebuiltString = str_replace('So', 'so', $rebuiltString);
	$rebuiltString = str_replace('Of', 'of', $rebuiltString);
	$rebuiltString = str_replace('In', 'in', $rebuiltString);
	$rebuiltString = str_replace('On', 'on', $rebuiltString);
	$rebuiltString = str_replace('With', 'with', $rebuiltString);

	//add first and last words back

	$rebuiltString = trim($firstWord." ".$rebuiltString." ".$lastWord);

	return $rebuiltString;

}

function array_glue($array1,$array2,$explodeChar='|',$searchString)
{
	//force each variable to be an array
	$array1[] = '';
	$array2[] = '';
	$returnArray = array_merge($array1,$array2);//merge the two arrays into one
	$returnArray = array_filter($returnArray);//remove the empty elements
	$returnArray = array_values($returnArray);//reindex the array
	$returnArray2 = $returnArray[0];//take the first element
	$returnArray2 = explode($explodeChar, $returnArray2);//split that into a new array
	$searchString = '/'.$searchString.'/';
	$returnArray2 = preg_grep($searchString,$returnArray2);//find the search string in the array
	$returnArray2 = array_values($returnArray2);//reindex again
	$returnVal = $returnArray2[0];//return the first part of the array
	return $returnVal;
}

function curl_build($url,$headers=0)
	//build a curl request with or without headers
{
	//construct curl request
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL,$url);
	if ($headers==1)
	{
		curl_setopt($ch, CURLOPT_HEADER, 1);
	}
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
	curl_setopt($ch, CURLOPT_TIMEOUT, 60);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
	$request = curl_exec($ch);
	return $request;
}

function print_var_name($var)
	//turn a variable into the text version of its name
{
	foreach($GLOBALS as $var_name => $value) {
		if ($value === $var) {
			return $var_name;
		}
	}

	return false;
}

function export_array($array,$name='')
	//print an array with its name
{
	if (is_array($array))
	{
		$name = print_var_name($array);
		echo "$name:<br/>";
		print_r($array);
		echo "<br/><hr/><br/>";
		unset($name);
	}
}

function validateDate($date, $format = 'Y-m-d H:i:s')
{
	$d = DateTime::createFromFormat($format, $date);
	return $d && $d->format($format) == $date;
}

function date_fix($dateString)
{
	//put possible date formats in an array
	//d F Y = 01 January 2009
	//F j, Y = January 9, 2012
	//F Y = February 1976
	$dateArray = array('d F Y', 'j F Y','F j, Y', 'F Y');
	$returnVal='';

	foreach ($dateArray as $dateObj)
	{
		//$date = date_create_from_format($dateObj, $dateString);
		//$dateVal = date_format($date,'d m Y');
		if ($dateObj == 'F Y')
			//if the day is missing, this causes problems
			//(e.g. if today is the 30th, it will output march if you give it february)
			//so we add the date as the 1st day of the month
			{
			$dateObj .= ' d';
			$dateString .= ' 01';
		}

		if (validateDate($dateString,$dateObj))//if the date format is valid
			{
			$date = date_create_from_format($dateObj, $dateString);//create a date object
			$returnVal = date_format($date,'Y');//convert to just a year, e.g. 2009
			break;//stop the loop if it's a date
		}
	}

	return $returnVal;
}
?> 